﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfSnqkGasAnalysis.IBusiness_impl;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis
{
    /// <summary>
    /// 有机硫，分页
    /// </summary>
    public partial class PageOrganicSulfur : Page
    {
        public PageOrganicSulfur()
        {
            InitializeComponent();
        }
        SulfurBusiness sulfurBusiness = new SulfurBusiness();
        SourceDictBusiness sourceBusiness = new SourceDictBusiness();   

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            GetPage(1);

        }

        public void GetPage(int p)
        {
            Organic_sulfur where = new Organic_sulfur();
            where.Samp_no = txtReportNo.Text.Trim();
            Task.Run(() => {
                var page = sulfurBusiness.GetPage(where, p, CommonConfig.PageSize);
                this.Dispatcher.Invoke( () =>
                {
                    this.dataGrid.DataContext = page.List;
                    this.pager.PageIndex = p;
                    this.pager.PageSize = 10;
                    this.pager.Total = page.Total;
                });
            });

        }

        //分页
        private void PageBar_PageIndexChanged(object sender, RoutedPropertyChangedEventArgs<int> e)
        {
            GetPage(this.pager.PageIndex);
        }

        //编辑
        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            var info = this.dataGrid.SelectedItem as Organic_sulfur;

            WindowsSulfurEdit windowsSulfurEdit = new WindowsSulfurEdit();
            windowsSulfurEdit.editInfo = info;
            windowsSulfurEdit.RefreshPage += WindowSulfurAdd_RefreshPage;
            windowsSulfurEdit.ShowDialog();
        }

        //打印
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            //选中的数据
            var info = this.dataGrid.SelectedItem as Organic_sulfur;

            //查询完整数据         
            OrganicSulfurView model =  sulfurBusiness.GetView(info.Id);

            //备注 ，标准。创建时间：2023-1-13 13:53:59
            var biaozhunList = sourceBusiness.GetListByGroup("sLiu_bz_remark_list", 1);
            biaozhunList = biaozhunList.OrderBy(g => g.Createtime).ToList();
            model.source_Dicts = biaozhunList;

            WindowProofreadPrintPreview ppv = new WindowProofreadPrintPreview("FlowDocumentSulfurPrint.xaml", model, new PrintGas_analysisDocument());
            ppv.ShowInTaskbar = false;
            ppv.Show();
        }

        //日志
        private void log_Click(object sender, RoutedEventArgs e)
        {
            var info = this.dataGrid.SelectedItem as Organic_sulfur;
            WindowSulfurStepLog log = new WindowSulfurStepLog();
            log.mainKEY = info.Id;
            log.ShowDialog();   
        }

        //校对
        private void proo_Click(object sender, RoutedEventArgs e)
        {
            var info = this.dataGrid.SelectedItem as Organic_sulfur;

            WindowsSulfurEdit windowsSulfurEdit = new WindowsSulfurEdit();
            windowsSulfurEdit.editInfo = info;
            windowsSulfurEdit.Tag = "jiaodui";
            windowsSulfurEdit.RefreshPage += WindowSulfurAdd_RefreshPage;
            windowsSulfurEdit.ShowDialog();
        }

        //审核
        private void check_Click(object sender, RoutedEventArgs e)
        {
            var info = this.dataGrid.SelectedItem as Organic_sulfur;

            WindowSulfurCheck  check = new WindowSulfurCheck();
            check.editInfo = info;
            check.RefreshPage += WindowSulfurAdd_RefreshPage;
            check.ShowDialog();
        }

        //删除
        private void del_Click(object sender, RoutedEventArgs e)
        {
            var info = this.dataGrid.SelectedItem as Organic_sulfur;
            if (info != null)
            {
                var resultOk = MessageBox.Show("确认删除【" + info.Samp_no + "】吗", "操作提示", MessageBoxButton.YesNo);
                if (resultOk != MessageBoxResult.Yes)
                {
                    e.Handled = true;
                    return;
                }
                var result = sulfurBusiness.Delete(info.Id);
                if (result.CodeEnum == ResultCode.Success)
                {
                    GetPage(this.pager.PageIndex);
                }
                ShowAlertHelpter.ShowAlert(result.Msg);
            }
        }

        //查询
        private void search_Click(object sender, RoutedEventArgs e)
        {
            GetPage(1);
        }

        //录入
        private void input_Click(object sender, RoutedEventArgs e)
        {
            WindowSulfurAdd windowSulfurAdd = new WindowSulfurAdd();
            windowSulfurAdd.RefreshPage += WindowSulfurAdd_RefreshPage;
            windowSulfurAdd.ShowDialog();
        }

        private void WindowSulfurAdd_RefreshPage(object? sender, EventArgs e)
        {
            GetPage(pager.PageIndex);
        }

    }
}
